package com.scs.application.modules.dept.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.modules.base.entity.Dept;
import com.scs.application.modules.base.mapper.DeptMapper;
import com.scs.application.modules.dept.dto.ApplyAutoDTO;
import com.scs.application.modules.dept.dto.ApplyItemVo;
import com.scs.application.modules.dept.entity.ApplyAuto;
import com.scs.application.modules.dept.service.ApplyAutoService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.wm.entity.Dist;
import com.scs.application.modules.wm.service.DistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/dept/apply_auto")
public class ApplyAutoController extends CrudController<ApplyAutoService, ApplyAuto>{
    @Autowired
    private DistService distService;

    @Autowired
    private DeptMapper deptMapper;

    /**
     * PDA 查询所有待响应的 数据
     */
    @GetMapping("/listApplyAutoPDA")
    public RestResult<List> listApplyAutoPDA(ApplyAuto applyAuto) {
        List result = service.listApplyAutoPDA(applyAuto);
        return RestResult.ok("查询成功", result);
    }

    @PostMapping("/autoCreateApply")
    public RestResult autoCreateApply() {
        service.autoCreateApply();
        return RestResult.ok();
    }


    /**业务选择时候使用
     *
     */
    @RequestMapping("/pageForTransfer")
    public PageResult pageForTransfer() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        IPage pageResult = service.pageForTransfer(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
    /**
     * 转调拨单
     * @param para
     * @return
     */
    @PostMapping("toTransfer")
    public RestResult toTransfer(@RequestBody List<ApplyAuto> applyAutos) {
        return RestResult.ok("OK", service.toTransfer(applyAutos));
    }


    /**
     * 填充开单数量
     * @param applyAutos
     * @return
     */
    @PostMapping("fillQty")
    public RestResult fillQty(@RequestBody List<ApplyAuto> applyAutos) {
        return RestResult.ok("操作成功",service.fillQty(applyAutos));
    }



    /**
     * 转配送单
     * @param para
     * @return
     */
    @PostMapping("toTransferPDA")
    public RestResult toTransferPDA(@RequestBody Map<String, String> para) {
        List<ApplyAuto> applyAutos = JSON.parseArray(para.get("applyAutos"), ApplyAuto.class);
        return RestResult.ok("操作成功", service.toTransfer(applyAutos));
    }

    /**
     * 转配送单
     * @param applyAutos
     * @return
     */
    @PostMapping("toDist")
    public RestResult toDist(@RequestBody List<ApplyItemVo> applyAutos) {
        return RestResult.ok("OK", service.toDist(applyAutos, null));
    }

    /**
     * 转配送单
     * @param para
     * @return
     */
    @PostMapping("toDistPDA")
    public RestResult toDistPDA(@RequestBody Map<String, String> para) {
        List<ApplyItemVo> applyAutos = JSON.parseArray(para.get("applyAutos"), ApplyItemVo.class);
        List<Dist> dist = service.toDist(applyAutos, para.get("targetStatus"));
        return RestResult.ok("OK", distService.getById(dist.get(0).getId()));
    }


    /**
     * 作废明细
     *
     * @param applyAutoDTO
     * @return
     */
    @PostMapping("respondClose")
    public RestResult respondClose(@RequestBody ApplyAutoDTO applyAutoDTO) {

        service.respondClose(applyAutoDTO.getApplyAutos(), applyAutoDTO.getValue());
        return RestResult.ok();
    }


    @GetMapping("/autoApplyDeptList")
    public RestResult autoApplyDeptList() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        List<Dept> deptList = service.autoApplyDeptList(queryContext);
        return RestResult.ok("",deptList);
    }

    @GetMapping("/autoTransferDeptList")
    public RestResult autoTransferDeptList() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        List<Dept> deptList = service.autoTransferDeptList(queryContext);
        return RestResult.ok("",deptList);
    }

//  整单据作废
    @PostMapping("close")
    @Deprecated
    public  RestResult close(@RequestBody Map<String, String> para){
//      获取请领单的id和作废原因
        String id = para.get("id");
        String remark = para.get("remark");
        return RestResult.ok("作废成功", service.close(id,remark));
    }

//  明细作废
    @PostMapping("closeItem")
    @Deprecated
    public  RestResult closeItem(@RequestBody Map<String, String> para){
//      获取请领明细的id和作废原因
        String id = para.get("id");
        String remark = para.get("remark");
        return RestResult.ok("作废成功", service.closeItem(id,remark));
    }

    //获取配送 需求科室列表
    @GetMapping("/getDistDemandDept")
    public RestResult<List> getDistDemandDept() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> mapParam = queryContext.getParams();

        QueryWrapper queryWrapper = Wrappers.<ApplyAuto>query();
        queryWrapper.select("dept_id as value,dept_name as label");
        queryWrapper.eq("ref_type","DIST");
        queryWrapper.gt("sku_qty_apply",0);
        queryWrapper.isNull("ref_id");
        queryWrapper.groupBy("dept_id");

        //过滤时间区间
        if (mapParam != null && mapParam.get("gmtApply") !=null){
            Object[] gmtApplyArr = (Object[]) mapParam.get("gmtApply");
            if (gmtApplyArr.length == 2) {
                queryWrapper.between("gmt_apply",gmtApplyArr[0],gmtApplyArr[1] );
            }
        }

        List<Map<String, Object>> list = service.listMaps(queryWrapper );
        return RestResult.ok("查询成功", list);
    }


    //获取采购 需求科室列表
    @GetMapping("/getPurchaseDemandDept")
    public RestResult<List> getPurchaseDemandDept() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> mapParam = queryContext.getParams();

        QueryWrapper queryWrapper = Wrappers.<ApplyAuto>query();
        queryWrapper.select("dept_id as value,dept_name as label");
        queryWrapper.eq("ref_type","PURCHASE");
        queryWrapper.gt("sku_qty_apply",0);
        queryWrapper.groupBy("dept_id");

        //过滤时间区间
        if (mapParam != null && mapParam.get("gmtApply") !=null){
            Object[] gmtApplyArr = (Object[]) mapParam.get("gmtApply");
            if (gmtApplyArr.length == 2) {
                queryWrapper.between("gmt_apply",gmtApplyArr[0],gmtApplyArr[1] );
            }
        }

        List<Map<String, Object>> list = service.listMaps(queryWrapper );
        return RestResult.ok("查询成功", list);
    }
}
